package co.baiku.boot.common.tools;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.nio.charset.Charset;

public class IOTools {
    private static Logger log = LoggerFactory.getLogger(IOTools.class);

    private IOTools() {
    }

    public static InputStream string2InputStream(String str, Charset charset) {
        InputStream in = null;
        try {
            in = new ByteArrayInputStream(str.getBytes(charset.name()));
        } catch (UnsupportedEncodingException e) {
            log.error(e.getMessage(), e);
        }
        return in;
    }

    public static String inputStream2String(InputStream is) throws IOException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        int i = -1;
        while ((i = is.read()) != -1) {
            baos.write(i);
        }
        return baos.toString(StringTools.UTF8.name());
    }
}
